package com.person.title.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.person.commons.utils.DAO;
import com.person.sys.model.SysFunc;
import com.person.title.model.Column;
import com.person.title.model.Title;
import com.person.title.model.TitleSub;
import com.person.title.service.TitleSubService;

public class TitleSubServiceImpl implements TitleSubService {

	private DAO dao;
	private static final char SUCCESS = '1';
	private static final char FAIL = '0';
	private static final char FAIL_1 = '2';//子栏目分类代码重复的提示返回
	private static final char FAIL_2 = '3';//子栏目分类名字重复的提示返回
	private static final char FAIL_3 = '4';//子栏目分类名字重复的提示返回

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}
	
	
	
	
	@Override
	public List<TitleSub> TitleSubList(String titleId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public char createTitleSub(TitleSub titleSub) {
		
		char forward = 0;
		String code = titleSub.getTitleCodeGroup() ;
		code = code.substring(0, code.length()-2) ;		
		
		//主菜单分类代码是否重复		
		String hql1 = "from TitleSub where titleCodeGroup = '"+titleSub.getTitleCodeGroup()+"'";
		List<Title> list1 = dao.find(hql1);
		
		//主菜单名字是否重复
		String hql2 = "from TitleSub where titleCodeGroup like '"+code+"__' and subName = '"+titleSub.getSubName()+"'";
		List<Title>  list2 = dao.find(hql2);
		
		//主菜单英文名字是否重复
		String hql3 = "from TitleSub where titleCodeGroup like '"+code+"__' and subNameEn = '"+titleSub.getSubNameEn()+"'";
		List<Title>  list3 = dao.find(hql3);
		if(list1.size() == 0 && list2.size() == 0 && list3.size() == 0){			
			this.dao.save(titleSub);
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}else if(list3.size() > 0){
				forward = FAIL_3;
			}
		}
		return forward;
	}

	@Override
	public char deleteTitleSub(TitleSub titleSub) {
		
		String hql = "from TitleSub where titleCodeGroup like '" + titleSub.getTitleCodeGroup() + "%'";
	    List<SysFunc> list = dao.find(hql);
	   
	    if(list.size() == 1){
	    	this.dao.delete(titleSub) ;    	
	    	return SUCCESS;
	    }else{
	    	
	    	return FAIL;
	    }	
	}

	@Override
	public List findTitleSubByCode(String[] titleCodeGroup) {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public TitleSub findTitleSubByCode(String titleCode) {
		String hql = "from TitleSub where titleCodeGroup = '" + titleCode+ "'" ;
		List titleSubs = this.dao.find(hql) ;
		TitleSub titleSub = null ;
		if (titleSubs.size()>0) {
			titleSub = (TitleSub) titleSubs.get(0) ;
		}
		return titleSub ;
	}
	
	
	@Override
	public TitleSub findTitleSubById(String subId) {		
		return (TitleSub) this.dao.find(TitleSub.class, subId) ;
	}

	@Override
	public char modifyTitleSub(TitleSub titleSub) {
		
		char forward = 0;
		String code = titleSub.getTitleCodeGroup() ;
		code = code.substring(0, code.length()-2) ;
		
		//子栏目分类代码是否重复
		String hql1 = "from TitleSub where titleCodeGroup = '"+titleSub.getTitleCodeGroup()+"' and subId != '"+titleSub.getSubId()+"'";
		List<Title> list1 = dao.find(hql1);
				
		//子栏目名字是否重复
		String hql2 = "from TitleSub where titleCodeGroup like '"+code+"__' and subName = '"+titleSub.getSubName()+"' and subId != '"+titleSub.getSubId()+"'";
		List<Title>  list2 = dao.find(hql2);
		
		//子栏目名字是否重复
		String hql3 = "from TitleSub where titleCodeGroup like '"+code+"__' and subNameEn = '"+titleSub.getSubNameEn()+"' and subId != '"+titleSub.getSubId()+"'";
		List<Title>  list3 = dao.find(hql3);
						
		if(list1.size() == 0 && list2.size() == 0&& list3.size() == 0){
			
			dao.update(titleSub);
				
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}else if(list3.size() > 0){
				forward = FAIL_3;
			}
		}
		return forward;
	}

	@Override
	public char modifyJuniorTitleSub(TitleSub titleSub, String titleCodeGroup) {
		String hql = "from TitleSub where titleCodeGroup like '" + titleCodeGroup + "__%' order by titleCodeGroup " ;
		List <TitleSub> titleSubs = this.dao.find(hql) ;	
		
		for (TitleSub titleSub1:titleSubs) {
			titleCodeGroup = titleSub1.getTitleCodeGroup() ;
			String titleCodeGroup1 = titleSub.getTitleCodeGroup() ;
			titleCodeGroup1 = titleCodeGroup1 + titleCodeGroup.substring(titleCodeGroup1.length(), titleCodeGroup.length()) ;
			titleSub1.setTitleCodeGroup(titleCodeGroup1) ;
			
			this.dao.update(titleSub1) ;
		}
		return SUCCESS ;
	}
	
	
	@Override
	public List<Column> titleSelectList() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public List<TitleSub> juniorTitleSubList(String subId) {

		TitleSub titleSub =(TitleSub) this.dao.find(TitleSub.class, subId) ;
		String hql = "from TitleSub where titleCodeGroup like '" + titleSub.getTitleCodeGroup() + "__' order by titleCodeGroup " ;
		List <TitleSub> titleSubs = this.dao.find(hql) ;	
		return titleSubs;
	}
	/**
	 * 根据组代码长度查找所有的二级或者三级栏目
	 */
	@SuppressWarnings("unchecked")
	public List<TitleSub> findAllTitleSub(int length){
		try{
			List<TitleSub> lt = dao.find("  from TitleSub where 1=1 order by titleCodeGroup");
			List<TitleSub> list = new ArrayList();
			for(int i=0;i<lt.size();i++){
				if(lt.get(i).getTitleCodeGroup().length()==length){
					list.add(lt.get(i));
				}
			}
			return list;
		}catch(Exception e){
			e.printStackTrace();
		}
		return new ArrayList();
	}
	/**
	 * 根据funcId查找一条记录
	 * @param funcId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TitleSub> findTitleSubByfuncId(String funcId){
		try{
			return dao.find("from TitleSub where funcId='"+funcId+"'");
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList();
		}
	}
	@SuppressWarnings("unchecked")
	public List<TitleSub> findTitleSubByTitleCodeGroup(String titleCode){
		try{
			return dao.find("from TitleSub where titleCodeGroup like '"+titleCode+"__' order by titleCodeGroup");
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList();
		}
	}
	public void update(TitleSub titleSub){
		dao.update(titleSub);
	}

}
